﻿// // ******************************************************************
// //       /\ /|       @file       LanguageConversionQueryHandler.cs
// //       \ V/        @brief      await29
// //       | "")       @author     Await, 285283010@qq.com
// //       /  |
// //      /  \\        @Modified   2025-01-01 21:01
// //    *(__\_\        @Copyright  Copyright (c) , 2025-01-01 21:01 Await
// // ******************************************************************

using AutomaticSubtitles.Service.Application.LanguageConversion.Queries;
using AutomaticSubtitles.Service.Domain.LanguageConversion;

namespace AutomaticSubtitles.Service.Application.LanguageConversion;

public class LanguageConversionQueryHandler(ILanguageConversionRepository repository) : BaseAutoService
{
    [EventHandler]
    public async Task Handler(GetLanguageConversionQuery query)
    {
        var data = (await repository.GetListAsync(x => query.data.Contains(x.Language))).ToList();
        var result = new Dictionary<string, List<string>>();
        foreach (var se in query.data) result.TryAdd(se, data.Where(x => x.Language == se).Select(x => x.Suffix)?.ToList() ?? [se]);
        query.Result = result;
    }

    [EventHandler]
    public async Task Handler(GetLanguageConversionToKeyQuery query)
    {
        var data = await repository.FindAsync(x => x.Suffix == query.data);
        query.Result = data?.Language;
    }

    [EventHandler]
    public async Task Handler(GetLanguageConversionListDataQuery query)
    {
        var data = await repository.GetListAsync();
        query.Result = data.ToDictionary(x => x.Suffix, x => x.Language);
    }
}